package com.jz.community.modulemine.integral.task;

import android.app.Activity;

import com.alibaba.fastjson.JSON;
import com.jz.community.basecomm.constant.Constant;
import com.jz.community.basecomm.net.OkHttpUtil;
import com.jz.community.basecomm.task.ITaskCallbackListener;
import com.jz.community.basecomm.task.RxTask;
import com.jz.community.basecomm.utils.BaseSpUtils;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.commview.view.widget.ProgressDialogManager;
import com.jz.community.modulemine.integral.bean.IntegralInfo;

import java.util.HashMap;

/**
 * Created by Tony.Fan on 2018/6/14 17:47
 * 积分
 */
public class GetIntegralTask extends RxTask<String, Integer, IntegralInfo> {

    private Activity activity;
    private ITaskCallbackListener taskListener;

    public GetIntegralTask(Activity activity,
                           ITaskCallbackListener taskListener) {
        super(activity);
        this.activity = activity;
        this.taskListener = taskListener;
    }

    @Override
    protected IntegralInfo doInBackground(String... params) {
        // 执行请求
        String result = OkHttpUtil.get(Constant.INTEGRAL_URL, getParam(params[0]));
        IntegralInfo info = null;
        if (!Preconditions.isNullOrEmpty(result)) {
            info =  JSON.parseObject(result, IntegralInfo.class);
        }
        return info;
    }

    @Override
    protected void onPostExecute(IntegralInfo result) {
        // 执行结果回调函数
        taskListener.doTaskComplete(result);
        ProgressDialogManager.dismissProgressDialog();
        super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute() {
        ProgressDialogManager.showDialog(activity);
        super.onPreExecute();
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
    }

    /**
     * 获取请求所需的参数
     *
     * @param
     * @return
     */
    private HashMap<String, Object> getParam(String page) {

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("page", page);
        hashMap.put("size", 10);
        hashMap.put("filter", "isuid::" + BaseSpUtils.getInstance().getUserId(activity));
        return hashMap;
    }
}